﻿#建议保存编码为：bom头 + utf8

param
(
	[parameter(Mandatory = $true)]
	[ValidateNotNullOrEmpty()]
	[Alias("ipaddress")][String]$目的ip地址,

	[Alias("port")][uint16]$端口 = 22,

	[parameter(Mandatory = $true)]
	[ValidateNotNullOrEmpty()]
	[Alias("user")][String]$用户名,

	[parameter(Mandatory = $true)]
	[ValidateNotNullOrEmpty()]
	[Alias("pwd")][String]$密码,

	[parameter(Mandatory = $true)]
	[ValidateNotNullOrEmpty()]
	[String]$LiteralPath,

	[parameter(Mandatory = $true)]
	[ValidateNotNullOrEmpty()]
	[String]$Destination,

	[Switch]$Recurse
	#缩水封装
)

if ( ($IsWindows -eq $True) -or ($PSVersionTable.psversion.major -lt 6) ) #win
{
	if ($env:LANG -eq 'zh_CN.UTF-8')
	{
		Write-Warning '错误：应该使用另一个脚本。错误码22'
	}
	else
	{
		Write-Warning 'Error: Another script should be used.exit 22'
	}
	exit 22
}

if ($IsLinux -eq $True)
{
	& '/etc/kasini3000/0k_source.ps1'
}

if ($IsLinux -eq $True)
{
	if ($env:LANG -eq 'zh_CN.UTF-8')
	{
		Write-Warning '错误：应该使用另一个脚本。'
	}
	else
	{
		Write-Warning 'Error: Another script should be used.'
	}
}

Write-Verbose '从主控机到被控机，复制文件开始'

if ($env:LANG -eq 'zh_CN.UTF-8')
{
	Write-Warning '本脚本限制：需要用户交互输入密码，无法自动。'
}
else
{
	Write-Warning 'Limitation of this script: It requires the user to enter the password interactively, not automatically.'
}

$端口通了 = & '/etc/kasini3000/tcp--ping-v2.ps1' -MyComputerName $目的ip地址 -port $端口 -Quiet
if ($端口通了 -eq $true)
{
}
else
{
	if ($env:LANG -eq 'zh_CN.UTF-8')
	{
		Write-Error "错误：被控机端口不通。错误码24"
	}
	else
	{
		Write-Error "error：node port cloesd.exit 24"
	}
	exit 24
}

if ($IsLinux -eq $True)
{
	Write-Verbose '使用ssh密码，连接开始'
	$private:连接l2l11 = New-PSSession -HostName ${目的ip地址} -Port $端口 -UserName $用户名
	if ( ($private:连接l2l11 -eq $null) -or ($private:连接l2l11 -eq '') )
	{
		if ($env:LANG -eq 'zh_CN.UTF-8')
		{
			Write-Error "错误：使用密码连接失败。错误码25"
		}
		else
		{
			Write-Error "error：node port cloesd.exit 25"
		}
		exit 25
	}

	Write-Verbose '连接成功。现在开始执行命令：'
	$主控机公钥sha1 = Get-FileHash -Algorithm sha1 -LiteralPath "${global:kasini3000目录}\ssh_key_files_old1\authorized_keys"
	$被控机公钥sha1 = Invoke-Command -Session $private:连接l2l11 -ScriptBlock { Get-FileHash -Algorithm sha1 -LiteralPath '/root/.ssh/authorized_keys' }
	Write-Verbose ("公钥： {0} '---' {1} " -f ${主控机公钥sha1}.Hash,${被控机公钥sha1}.Hash)
	if ($主控机公钥sha1.Hash -ne $被控机公钥sha1.Hash)
	{
		Write-Verbose "主控机-被控机之间，公钥有不同"
		Copy-Item -LiteralPath "${global:kasini3000目录}\ssh_key_files_old1\authorized_keys" -Destination '/root/.ssh/' -ToSession $private:连接l2l11 -Force
	}

	[scriptblock]$private:cmd1 = `
	{
		Test-Path -LiteralPath '/etc/kasini3000'
	}

	$private:返回 = Invoke-Command -Session $private:连接l2l11 -ScriptBlock $private:cmd1
	if ($private:返回 -eq $true)
	{
	}
	else
	{
		[scriptblock]$private:cmd2 = `
		{
			mkdir '/etc/kasini3000'
		}
		Invoke-Command -Session $private:连接l2l11 -ScriptBlock $private:cmd2
		$env:LANG = 'zh_CN.UTF-8'
		Copy-Item -LiteralPath 'c:\ProgramData\kasini3000\node_script' -Destination '/etc/kasini3000' -Recurse -ToSession $private:连接l2l11
	}
	Invoke-Command -Session $private:连接l2l11 -ScriptBlock { & '/etc/kasini3000/node_script/kasini3000/tj在被控机添加path.ps1' }

	Write-Verbose '连接成功。现在开始复制文件：'
	if ($Recurse -eq $True)
	{
		Copy-Item -FromSession $private:连接l2l11 -LiteralPath $LiteralPath -Destination $Destination -Force -Recurse
	}
	else
	{
		Copy-Item -FromSession $private:连接l2l11 -LiteralPath $LiteralPath -Destination $Destination -Force
	}
	Write-Verbose '复制文件完成，即将断开连接。'
	Remove-PSSession -Session $private:连接l2l11
}

Write-Verbose '从主控机到被控机，复制文件完成'

exit 0
